import * as fs from 'fs';
import * as Koa from 'koa';
import { Context } from 'koa';
import * as koaBody from 'koa-body';
import * as Router from 'koa-router';
import * as path from 'path';
import config from './config';
import { loadModels } from './model/model';
import loadRoutes from './routes/routes';
import logger from './plugin/logger';
import * as validate from 'koa-validate';
import * as staticCache from 'koa-static-cache';

const app = new Koa();

app.use(logger());
validate(app);
app.use(koaBody({
  multipart: true,
  formidable: {
    uploadDir: path.resolve('.', 'dist', 'public', 'upload'),
    multiples: false,
    keepExtensions: true
  }
}));
app.use(staticCache(path.resolve(__dirname, 'public'), {
  dynamic: true,
  prefix: config.prefix.static,
}));
loadModels();
loadRoutes(app);

export default app;
